<template>
  <el-container class="el-container">

    <el-main class="el-container1">
      <el-header class="header">

        <h1 class="header-language">用户注册页</h1>

      </el-header>
      <div class="el-main-register">
        <el-form :model="Register" status-icon :rules="rules" ref="Register" label-width="100px" class="demo-Register" >

          <el-form-item label="用户名" prop="username">
            <el-input v-model="Register.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="Register.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="Register.checkPass" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="性别">
            <div>
              <el-radio-group v-model="Register.sex">
                <el-radio label="1">男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </div>

          </el-form-item>
          <!--
                    <el-form-item label="年龄" prop="age">
                      <el-input v-model.number="Register.age"></el-input>
                    </el-form-item>-->

          <el-form-item label="生日">
            <el-date-picker
              v-model="Register.brithday"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(Register)">提交</el-button>
            <el-button @click="resetForm('Register')">重置</el-button>


          </el-form-item>
        </el-form>
      </div>

    </el-main>
  </el-container>
</template>

<script>

import {register} from "../api";
export default {
  name: "Register",
  data() {
    /*var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      }, 1000);
    };*/
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }
      else if (value.length!= 8){
        callback(new Error('请输入8位密码'))
      }

      else {
        if (this.Register.checkPass !== '') {


          this.$refs.Register.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      }
      else if (value.length!= 8){
        callback(new Error('请输入8位密码'))
      }
      else if (value !== this.Register.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      Register: {
        username: '',
        password: '',
        checkPass: '',
        sex: '1',
        // age: '',
        brithday: ''
      },
      rules: {
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ]/*,
        age: [
          {validator: checkAge, trigger: 'blur'}
        ]*/
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.Register.validate(async (valid) => {
        if (valid) {
          console.log(this.Register)
          // this.$http.post("register",this.Register)
          const result = await register(this.Register)

          if (result.data.code == 200) {
            this.$message({
              message: '注册成功，请登录',
              type: "success"
            })
            this.$router.push("/login")
          } else {
            this.$refs.Register.resetFields()
            this.$message({
              message: '注册失败，请重新注册',
              type: "error"
            })
          }
          alert('注册成功，即将跳转到登录页进行登录!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.el-container {
  height: 100%;
  width: 100%;
  background-image: url("../assets/img/img_2.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

.header {
  /*background-color: #B3C0D1;*/
  /*color: #333;*/
  text-align: center;
  line-height: 60px;
  font-size: 20px;
  margin: 0 1px;
  /*font-family: Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;*/
}

.header-language {
  text-shadow: -1px -1px 1px #aaa,
  0px 4px 1px rgba(192, 36, 36, 0.5),
  4px 4px 5px rgba(0, 0, 0, 0.7),
  0px 0px 7px rgba(23, 62, 121, 0.4);

  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif
}

.el-container1 {
  padding: 10px 30px 10px 0px;
  width: 600px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50px;
  box-shadow: 0 2px 12px 0 rgba(19, 19, 19, 0.1);
  /*background-color: #820ea2;*/
}

.el-main-register {
  width: 500px;
  margin: auto;
  /*border: 1px solid black;*/
  /*border-radius: 5px;*/
  font-size: 20px;
}
</style>
